home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / libtiff / tif_dirread.c < prev    next >
C/C++ Source or Header  |  1992-03-24  |  32KB  |  1,202 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_dirread.c,v 1.20 92/03/25 09:58:09 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * Directory Read Support Routines.
  33.  *
  34.  * NB: Beware of the varargs declarations for routines in
  35.  *     this file.  The names and types of variables has been
  36.  *     carefully chosen to make things work with compilers that
  37.  *     are busted in one way or another (e.g. SGI/MIPS).
  38.  */
  39. #include "tiffioP.h"
  40.  
  41. #define    IGNORE    0        /* tag placeholder used below */
  42.  
  43. #if HAVE_IEEEFP
  44. #define    TIFFCvtIEEEFloatToNative(tif, n, fp)
  45. #endif
  46.  
  47. #include "prototypes.h"
  48. #if USE_PROTOTYPES
  49. static    EstimateStripByteCounts(TIFF *, TIFFDirEntry *, u_int);
  50. static    MissingRequired(TIFF *, char *);
  51. static    CheckDirCount(TIFF *, TIFFDirEntry *, u_long);
  52. static    TIFFFetchData(TIFF *, TIFFDirEntry *, char *);
  53. static    TIFFFetchString(TIFF *, TIFFDirEntry *, char *);
  54. static    float TIFFFetchRational(TIFF *, TIFFDirEntry *);
  55. static    TIFFFetchNormalTag(TIFF *, TIFFDirEntry *);
  56. static    TIFFFetchPerSampleShorts(TIFF *, TIFFDirEntry *, long *);
  57. static    TIFFFetchShortArray(TIFF *, TIFFDirEntry *, u_short []);
  58. static    TIFFFetchStripThing(TIFF *, TIFFDirEntry *, long, u_long **);
  59. static    TIFFFetchRefBlackWhite(TIFF *, TIFFDirEntry *);
  60. static    TIFFFetchJPEGQTables(TIFF *, TIFFDirEntry *);
  61. static    TIFFFetchJPEGCTables(TIFF *, TIFFDirEntry *, u_char ***);
  62. static    TIFFFetchExtraSamples(TIFF *, TIFFDirEntry *);
  63. static    float TIFFFetchFloat(TIFF *, TIFFDirEntry *);
  64. static    int TIFFFetchFloatArray(TIFF *, TIFFDirEntry *, float *);
  65. extern    int TIFFSetCompressionScheme(TIFF *, int);
  66. extern    int TIFFDefaultDirectory(TIFF*);
  67. extern    int TIFFFreeDirectory(TIFF*);
  68. #else
  69. static    EstimateStripByteCounts();
  70. static    MissingRequired();
  71. static    CheckDirCount();
  72. static    TIFFFetchData();
  73. static    TIFFFetchString();
  74. static    float TIFFFetchRational();
  75. static    TIFFFetchNormalTag();
  76. static    TIFFFetchPerSampleShorts();
  77. static    TIFFFetchShortArray();
  78. static    TIFFFetchStripThing();
  79. static    TIFFFetchRefBlackWhite();
  80. static    TIFFFetchJPEGQTables();
  81. static    TIFFFetchJPEGCTables();
  82. static    TIFFFetchExtraSamples();
  83. static    float TIFFFetchFloat();
  84. static    int TIFFFetchFloatArray();
  85. extern    int TIFFSetCompressionScheme();
  86. extern    int TIFFDefaultDirectory();
  87. extern    int TIFFFreeDirectory();
  88. #endif
  89.  
  90. static char *
  91. CheckMalloc(tif, n, what)
  92.     TIFF *tif;
  93.     int n;
  94.     char *what;
  95. {
  96.     char *cp = malloc(n);
  97.     if (cp == NULL)
  98.         TIFFError(tif->tif_name, "No space %s", what);
  99.     return (cp);
  100. }
  101.  
  102. /*
  103.  * Read the next TIFF directory from a file
  104.  * and convert it to the internal format.
  105.  * We read directories sequentially.
  106.  */
  107. TIFFReadDirectory(tif)
  108.     TIFF *tif;
  109. {
  110.     register TIFFDirEntry *dp;
  111.     register int n;
  112.     register TIFFDirectory *td;
  113.     TIFFDirEntry *dir;
  114.     long v;
  115.     TIFFFieldInfo *fip;
  116.     u_short dircount;
  117.     char *cp;
  118.     int diroutoforderwarning = 0;
  119.  
  120.     tif->tif_diroff = tif->tif_nextdiroff;
  121.     if (tif->tif_diroff == 0)        /* no more directories */
  122.         return (0);
  123.     tif->tif_curdir++;
  124.     if (!isMapped(tif)) {
  125.         if (!SeekOK(tif->tif_fd, tif->tif_diroff)) {
  126.             TIFFError(tif->tif_name,
  127.                 "Seek error accessing TIFF directory");
  128.             return (0);
  129.         }
  130.         if (!ReadOK(tif->tif_fd, &dircount, sizeof (short))) {
  131.             TIFFError(tif->tif_name,
  132.                 "Can not read TIFF directory count");
  133.             return (0);
  134.         }
  135.         if (tif->tif_flags & TIFF_SWAB)
  136.             TIFFSwabShort(&dircount);
  137.         dir = (TIFFDirEntry *)CheckMalloc(tif,
  138.             dircount * sizeof (TIFFDirEntry), "to read TIFF directory");
  139.         if (dir == NULL)
  140.             return (0);
  141.         if (!ReadOK(tif->tif_fd, dir, dircount*sizeof (TIFFDirEntry))) {
  142.             TIFFError(tif->tif_name, "Can not read TIFF directory");
  143.             goto bad;
  144.         }
  145.         /*
  146.          * Read offset to next directory for sequential scans.
  147.          */
  148.         if (!ReadOK(tif->tif_fd, &tif->tif_nextdiroff, sizeof (long)))
  149.             tif->tif_nextdiroff = 0;
  150. #ifdef MMAP_SUPPORT
  151.     } else {
  152.         off_t off = tif->tif_diroff;
  153.  
  154.         if (off + sizeof (short) > tif->tif_size) {
  155.             TIFFError(tif->tif_name,
  156.                 "Can not read TIFF directory count");
  157.             return (0);
  158.         } else
  159.             bcopy(tif->tif_base + off, &dircount, sizeof (short));
  160.         off += sizeof (short);
  161.         if (tif->tif_flags & TIFF_SWAB)
  162.             TIFFSwabShort(&dircount);
  163.         dir = (TIFFDirEntry *)CheckMalloc(tif,
  164.             dircount * sizeof (TIFFDirEntry), "to read TIFF directory");
  165.         if (dir == NULL)
  166.             return (0);
  167.         if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
  168.             TIFFError(tif->tif_name, "Can not read TIFF directory");
  169.             goto bad;
  170.         } else
  171.             bcopy(tif->tif_base + off, dir,
  172.                 dircount*sizeof (TIFFDirEntry));
  173.         off += dircount* sizeof (TIFFDirEntry);
  174.         if (off + sizeof (long) < tif->tif_size)
  175.             bcopy(tif->tif_base + off, &tif->tif_nextdiroff,
  176.                 sizeof (long));
  177.         else
  178.             tif->tif_nextdiroff = 0;
  179. #endif
  180.     }
  181.     if (tif->tif_flags & TIFF_SWAB)
  182.         TIFFSwabLong((u_long *)&tif->tif_nextdiroff);
  183.  
  184.     tif->tif_flags &= ~TIFF_BEENWRITING;    /* reset before new dir */
  185.     /*
  186.      * Setup default value and then make a pass over
  187.      * the fields to check type and tag information,
  188.      * and to extract info required to size data
  189.      * structures.  A second pass is made afterwards
  190.      * to read in everthing not taken in the first pass.
  191.      */
  192.     td = &tif->tif_dir;
  193.     /* free any old stuff and reinit */
  194.     TIFFFreeDirectory(tif);
  195.     TIFFDefaultDirectory(tif);
  196.     /*
  197.      * Electronic Arts writes gray-scale TIFF files
  198.      * without a PlanarConfiguration directory entry.
  199.      * Thus we setup a default value here, even though
  200.      * the TIFF spec says there is no default value.
  201.      */
  202.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  203.     for (fip = tiffFieldInfo, dp = dir, n = dircount; n > 0; n--, dp++) {
  204.         if (tif->tif_flags & TIFF_SWAB) {
  205.             TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
  206.             TIFFSwabArrayOfLong(&dp->tdir_count, 2);
  207.         }
  208.         /*
  209.          * Find the field information entry for this tag.
  210.          */
  211.         /*
  212.          * Silicon Beach (at least) writes unordered
  213.          * directory tags (violating the spec).  Handle
  214.          * it here, but be obnoxious (maybe they'll fix it?).
  215.          */
  216.         if (dp->tdir_tag < fip->field_tag) {
  217.             if (!diroutoforderwarning) {
  218.                 TIFFWarning(tif->tif_name,
  219.     "invalid TIFF directory; tags are not sorted in ascending order");
  220.                 diroutoforderwarning = 1;
  221.             }
  222.             fip = tiffFieldInfo;    /* O(n^2) */
  223.         }
  224.         while (fip->field_tag && fip->field_tag < dp->tdir_tag)
  225.             fip++;
  226.         if (!fip->field_tag || fip->field_tag != dp->tdir_tag) {
  227.             TIFFWarning(tif->tif_name,
  228.                 "unknown field with tag %d (0x%x) ignored",
  229.                 dp->tdir_tag,  dp->tdir_tag);
  230.             dp->tdir_tag = IGNORE;
  231.             fip = tiffFieldInfo;    /* restart search */
  232.             continue;
  233.         }
  234.         /*
  235.          * Null out old tags that we ignore.
  236.          */
  237.         if (fip->field_bit == FIELD_IGNORE) {
  238.     ignore:
  239.             dp->tdir_tag = IGNORE;
  240.             continue;
  241.         }
  242.         /*
  243.          * Check data type.
  244.          */
  245.         while (dp->tdir_type != (u_short)fip->field_type) {
  246.             if (fip->field_type == TIFF_ANY)    /* wildcard */
  247.                 break;
  248.             fip++;
  249.             if (!fip->field_tag || fip->field_tag != dp->tdir_tag) {
  250.                 TIFFWarning(tif->tif_name,
  251.                    "wrong data type %d for \"%s\"; tag ignored",
  252.                     dp->tdir_type, fip[-1].field_name);
  253.                 goto ignore;
  254.             }
  255.         }
  256.         /*
  257.          * Check count if known in advance.
  258.          */
  259.         if (fip->field_readcount != TIFF_VARIABLE) {
  260.             u_long expected = (fip->field_readcount == TIFF_SPP) ?
  261.                 (u_long) td->td_samplesperpixel :
  262.                 (u_long) fip->field_readcount;
  263.             if (!CheckDirCount(tif, dp, expected))
  264.                 goto ignore;
  265.         }
  266.  
  267.         switch (dp->tdir_tag) {
  268.         case TIFFTAG_STRIPOFFSETS:
  269.         case TIFFTAG_STRIPBYTECOUNTS:
  270.         case TIFFTAG_TILEOFFSETS:
  271.         case TIFFTAG_TILEBYTECOUNTS:
  272.             TIFFSetFieldBit(tif, fip->field_bit);
  273.             break;
  274.         case TIFFTAG_IMAGEWIDTH:
  275.         case TIFFTAG_IMAGELENGTH:
  276.         case TIFFTAG_IMAGEDEPTH:
  277.         case TIFFTAG_TILELENGTH:
  278.         case TIFFTAG_TILEWIDTH:
  279.         case TIFFTAG_TILEDEPTH:
  280.         case TIFFTAG_PLANARCONFIG:
  281.         case TIFFTAG_SAMPLESPERPIXEL:
  282.         case TIFFTAG_ROWSPERSTRIP:
  283.             if (!TIFFFetchNormalTag(tif, dp))
  284.                 goto bad;
  285.             break;
  286.         }
  287.     }
  288.  
  289.     /*
  290.      * Allocate directory structure and setup defaults.
  291.      */
  292.     if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
  293.         MissingRequired(tif, "ImageLength");
  294.         goto bad;
  295.     }
  296.     if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
  297.         MissingRequired(tif, "PlanarConfiguration");
  298.         goto bad;
  299.     }
  300.     /* 
  301.       * Setup appropriate structures (by strip or by tile)
  302.      */
  303.     if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  304.         td->td_stripsperimage = (td->td_rowsperstrip == 0xffffffff ?
  305.              (td->td_imagelength != 0 ? 1 : 0) :
  306.              howmany(td->td_imagelength, td->td_rowsperstrip));
  307.         td->td_tilewidth = td->td_imagewidth;
  308.         td->td_tilelength = td->td_rowsperstrip;
  309.         td->td_tiledepth = td->td_imagedepth;
  310.         tif->tif_flags &= ~TIFF_ISTILED;
  311.     } else {
  312.         td->td_stripsperimage = TIFFNumberOfTiles(tif);
  313.         tif->tif_flags |= TIFF_ISTILED;
  314.     }
  315.     td->td_nstrips = td->td_stripsperimage;
  316.     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  317.         td->td_nstrips *= td->td_samplesperpixel;
  318.     if (td->td_nstrips > 0 && !TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
  319.         MissingRequired(tif,
  320.             isTiled(tif) ? "TileOffsets" : "StripOffsets");
  321.         goto bad;
  322.     }
  323.  
  324.     /*
  325.      * Second pass: extract other information.
  326.      */
  327.     for (dp = dir, n = dircount; n > 0; n--, dp++) {
  328.         if (dp->tdir_tag == IGNORE)
  329.             continue;
  330.         switch (dp->tdir_tag) {
  331.         case TIFFTAG_COMPRESSION:
  332.         case TIFFTAG_MINSAMPLEVALUE:
  333.         case TIFFTAG_MAXSAMPLEVALUE:
  334.         case TIFFTAG_BITSPERSAMPLE:
  335.             /*
  336.              * The 5.0 spec says the Compression tag has
  337.              * one value, while earlier specs say it has
  338.              * one value per sample.  Because of this, we
  339.              * accept the tag if one value is supplied.
  340.              *
  341.              * The MinSampleValue, MaxSampleValue and
  342.              * BitsPerSample tags are supposed to be written
  343.              * as one value/sample, but some vendors incorrectly
  344.              * write one value only -- so we accept that
  345.              * as well (yech).
  346.              */
  347.             if (dp->tdir_count == 1) {
  348.                 v = TIFFExtractData(tif,
  349.                     dp->tdir_type, dp->tdir_offset);
  350.                 if (!TIFFSetField(tif, dp->tdir_tag, (int)v))
  351.                     goto bad;
  352.                 break;
  353.             }
  354.             /* fall thru... */
  355.         case TIFFTAG_DATATYPE:
  356.         case TIFFTAG_SAMPLEFORMAT:
  357.             if (!TIFFFetchPerSampleShorts(tif, dp, &v) ||
  358.                 !TIFFSetField(tif, dp->tdir_tag, (int)v))
  359.                 goto bad;
  360.             break;
  361.         case TIFFTAG_STRIPOFFSETS:
  362.         case TIFFTAG_TILEOFFSETS:
  363.             if (!TIFFFetchStripThing(tif, dp,
  364.                 td->td_nstrips, &td->td_stripoffset))
  365.                 goto bad;
  366.             break;
  367.         case TIFFTAG_STRIPBYTECOUNTS:
  368.         case TIFFTAG_TILEBYTECOUNTS:
  369.             if (!TIFFFetchStripThing(tif, dp,
  370.                 td->td_nstrips, &td->td_stripbytecount))
  371.                 goto bad;
  372.             break;
  373.         case TIFFTAG_IMAGELENGTH:
  374.         case TIFFTAG_ROWSPERSTRIP:
  375.         case TIFFTAG_TILELENGTH:
  376.         case TIFFTAG_TILEWIDTH:
  377.         case TIFFTAG_TILEDEPTH:
  378.         case TIFFTAG_SAMPLESPERPIXEL:
  379.         case TIFFTAG_PLANARCONFIG:
  380.             /* handled in first pass above */
  381.             break;
  382.         case TIFFTAG_COLORMAP:
  383.             if (!CheckDirCount(tif,dp,3*(1L<<td->td_bitspersample)))
  384.                 break;
  385.             /* fall thru... */
  386.         case TIFFTAG_TRANSFERFUNCTION:
  387.             v = (1L<<td->td_bitspersample) * sizeof (u_short);
  388.             cp = CheckMalloc(tif,
  389.                 dp->tdir_count * sizeof (u_short),
  390.                 "to read \"TransferFunction\" tag");
  391.             if (cp != NULL) {
  392.                 if (TIFFFetchData(tif, dp, cp)) {
  393.                     /*
  394.                      * This deals with there being only
  395.                      * one array to apply to all samples.
  396.                      */
  397.                     if (dp->tdir_count == 1L<<td->td_bitspersample)
  398.                         v = 0;
  399.                     /* NB: we assume samples/pixel <= 4 */
  400.                     TIFFSetField(tif, dp->tdir_tag,
  401.                         cp, cp+v, cp+2*v, cp+3*v);
  402.                 }
  403.                 free(cp);
  404.             }
  405.             break;
  406.         case TIFFTAG_PAGENUMBER:
  407.             if (TIFFFetchShortArray(tif, dp, td->td_pagenumber))
  408.                 TIFFSetFieldBit(tif, FIELD_PAGENUMBER);
  409.             break;
  410.         case TIFFTAG_HALFTONEHINTS:
  411.             if (TIFFFetchShortArray(tif, dp, td->td_halftonehints))
  412.                 TIFFSetFieldBit(tif, FIELD_HALFTONEHINTS);
  413.             break;
  414. #ifdef COLORIMETRY_SUPPORT
  415.         case TIFFTAG_REFERENCEBLACKWHITE:
  416.             (void) TIFFFetchRefBlackWhite(tif, dp);
  417.             break;
  418. #endif
  419. #ifdef YCBCR_SUPPORT
  420.         case TIFFTAG_YCBCRSUBSAMPLING:
  421.             if (TIFFFetchShortArray(tif, dp, td->td_ycbcrsubsampling))
  422.                 TIFFSetFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
  423.             break;
  424. #endif
  425. #ifdef CMYK_SUPPORT
  426.         case TIFFTAG_DOTRANGE:
  427.             if (TIFFFetchShortArray(tif, dp, td->td_dotrange))
  428.                 TIFFSetFieldBit(tif, FIELD_DOTRANGE);
  429.             break;
  430. #endif
  431. #ifdef JPEG_SUPPORT
  432.         case TIFFTAG_JPEGQTABLES:
  433.             if (TIFFFetchJPEGQTables(tif, dp))
  434.                 TIFFSetFieldBit(tif, FIELD_JPEGQTABLES);
  435.             break;
  436.         case TIFFTAG_JPEGDCTABLES:
  437.             if (TIFFFetchJPEGCTables(tif, dp, &td->td_dctab))
  438.                 TIFFSetFieldBit(tif, FIELD_JPEGDCTABLES);
  439.             break;
  440.         case TIFFTAG_JPEGACTABLES:
  441.             if (TIFFFetchJPEGCTables(tif, dp, &td->td_actab))
  442.                 TIFFSetFieldBit(tif, FIELD_JPEGACTABLES);
  443.             break;
  444. #endif
  445.         case TIFFTAG_EXTRASAMPLES:
  446.             (void) TIFFFetchExtraSamples(tif, dp);
  447.             break;
  448. /* BEGIN REV 4.0 COMPATIBILITY */
  449.         case TIFFTAG_OSUBFILETYPE:
  450.             v = 0;
  451.             switch (TIFFExtractData(tif, dp->tdir_type,
  452.                 dp->tdir_offset)) {
  453.             case OFILETYPE_REDUCEDIMAGE:
  454.                 v = FILETYPE_REDUCEDIMAGE;
  455.                 break;
  456.             case OFILETYPE_PAGE:
  457.                 v = FILETYPE_PAGE;
  458.                 break;
  459.             }
  460.             if (v)
  461.                 (void) TIFFSetField(tif,
  462.                     TIFFTAG_SUBFILETYPE, (int)v);
  463.             break;
  464. /* END REV 4.0 COMPATIBILITY */
  465.         default:
  466.             (void) TIFFFetchNormalTag(tif, dp);
  467.             break;
  468.         }
  469.     }
  470.     /*
  471.      * Verify Palette image has a Colormap.
  472.      */
  473.     if (td->td_photometric == PHOTOMETRIC_PALETTE &&
  474.         !TIFFFieldSet(tif, FIELD_COLORMAP)) {
  475.         MissingRequired(tif, "Colormap");
  476.         goto bad;
  477.     }
  478.     /*
  479.      * Attempt to deal with a missing StripByteCounts tag.
  480.      */
  481.     if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
  482.         /*
  483.          * Some manufacturers violate the spec by not giving
  484.          * the size of the strips.  In this case, assume there
  485.          * is one uncompressed strip of data.
  486.          */
  487.         if (td->td_nstrips > 1) {
  488.             MissingRequired(tif, "StripByteCounts");
  489.             goto bad;
  490.         }
  491.         TIFFWarning(tif->tif_name,
  492. "TIFF directory is missing required \"%s\" field, calculating from imagelength",
  493.             TIFFFieldWithTag(TIFFTAG_STRIPBYTECOUNTS)->field_name);
  494.         EstimateStripByteCounts(tif, dir, dircount);
  495.     } else if (td->td_nstrips == 1 && td->td_stripbytecount[0] == 0) {
  496.         /*
  497.          * Plexus (and others) sometimes give a value
  498.          * of zero for a tag when they don't know what
  499.          * the correct value is!  Try and handle the
  500.          * simple case of estimating the size of a one
  501.          * strip image.
  502.          */
  503.         TIFFWarning(tif->tif_name,
  504. "Bogus \"%s\" field, ignoring and calculating from imagelength",
  505.             TIFFFieldWithTag(TIFFTAG_STRIPBYTECOUNTS)->field_name);
  506.         EstimateStripByteCounts(tif, dir, dircount);
  507.     }
  508.     if (dir)
  509.         free((char *)dir);
  510.     if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  511.         td->td_maxsamplevalue = (1L<<td->td_bitspersample)-1;
  512.     /*
  513.      * Setup default compression scheme.
  514.      */
  515.     if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
  516.         TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  517.     /*
  518.      * Reinitialize i/o since we are starting on a new directory.
  519.      */
  520.     tif->tif_row = -1;
  521.     tif->tif_curstrip = -1;
  522.     tif->tif_col = -1;
  523.     tif->tif_curtile = -1;
  524.     tif->tif_tilesize = TIFFTileSize(tif);
  525.     tif->tif_scanlinesize = TIFFScanlineSize(tif);
  526.     return (1);
  527. bad:
  528.     if (dir)
  529.         free((char *)dir);
  530.     return (0);
  531. }
  532.  
  533. static
  534. EstimateStripByteCounts(tif, dir, dircount)
  535.     TIFF *tif;
  536.     TIFFDirEntry *dir;
  537.     u_int dircount;
  538. {
  539.     register TIFFDirEntry *dp;
  540.     register TIFFDirectory *td = &tif->tif_dir;
  541.     register int n;
  542.  
  543.     td->td_stripbytecount = (u_long *)
  544.         CheckMalloc(tif, sizeof (u_long), "for \"StripByteCounts\" array");
  545.     if (td->td_compression != COMPRESSION_NONE) {
  546.         u_long space = sizeof (TIFFHeader)
  547.             + sizeof (short)
  548.             + (dircount * sizeof (TIFFDirEntry))
  549.             + sizeof (long);
  550.         long filesize = TIFFGetFileSize(tif->tif_fd);
  551.         /* calculate amount of space used by indirect values */
  552.         for (dp = dir, n = dircount; n > 0; n--, dp++) {
  553.             int cc = dp->tdir_count * tiffDataWidth[dp->tdir_type];
  554.             if (cc > sizeof (long))
  555.                 space += cc;
  556.         }
  557.         td->td_stripbytecount[0] = filesize - space;
  558.         /*
  559.          * This gross hack handles the case were the offset to
  560.          * the strip is past the place where we think the strip
  561.          * should begin.  Since a strip of data must be contiguous,
  562.          * it's safe to assume that we've overestimated the amount
  563.          * of data in the strip and trim this number back accordingly.
  564.          */ 
  565.         if (td->td_stripoffset[0] + td->td_stripbytecount[0] > filesize)
  566.             td->td_stripbytecount[0] =
  567.                 filesize - td->td_stripoffset[0];
  568.     } else {
  569.         u_long rowbytes = howmany(td->td_bitspersample *
  570.             td->td_samplesperpixel * td->td_imagewidth, 8);
  571.         td->td_stripbytecount[0] = td->td_imagelength * rowbytes;
  572.     }
  573.     TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  574.     if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  575.         td->td_rowsperstrip = td->td_imagelength;
  576. }
  577.  
  578. static
  579. MissingRequired(tif, tagname)
  580.     TIFF *tif;
  581.     char *tagname;
  582. {
  583.     TIFFError(tif->tif_name,
  584.         "TIFF directory is missing required \"%s\" field", tagname);
  585. }
  586.  
  587. /*
  588.  * Check the count field of a directory
  589.  * entry against a known value.  The caller
  590.  * is expected to skip/ignore the tag if
  591.  * there is a mismatch.
  592.  */
  593. static
  594. CheckDirCount(tif, dir, count)
  595.     TIFF *tif;
  596.     TIFFDirEntry *dir;
  597.     u_long count;
  598. {
  599.     if (count != dir->tdir_count) {
  600.         TIFFWarning(tif->tif_name,
  601.     "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
  602.             TIFFFieldWithTag(dir->tdir_tag)->field_name,
  603.             dir->tdir_count, count);
  604.         return (0);
  605.     }
  606.     return (1);
  607. }
  608.  
  609. /*
  610.  * Fetch a contiguous directory item.
  611.  */
  612. static
  613. TIFFFetchData(tif, dir, cp)
  614.     TIFF *tif;
  615.     TIFFDirEntry *dir;
  616.     char *cp;
  617. {
  618.     int cc, w;
  619.  
  620.     w = tiffDataWidth[dir->tdir_type];
  621.     cc = dir->tdir_count * w;
  622.     if (!isMapped(tif)) {
  623.         if (!SeekOK(tif->tif_fd, dir->tdir_offset))
  624.             goto bad;
  625.         if (!ReadOK(tif->tif_fd, cp, cc))
  626.             goto bad;
  627. #ifdef MMAP_SUPPORT
  628.     } else {
  629.         if (dir->tdir_offset + cc > tif->tif_size)
  630.             goto bad;
  631.         bcopy(tif->tif_base + dir->tdir_offset, cp, cc);
  632. #endif
  633.     }
  634.     if (tif->tif_flags & TIFF_SWAB) {
  635.         switch (dir->tdir_type) {
  636.         case TIFF_SHORT:
  637.         case TIFF_SSHORT:
  638.             TIFFSwabArrayOfShort((u_short *)cp, dir->tdir_count);
  639.             break;
  640.         case TIFF_LONG:
  641.         case TIFF_SLONG:
  642.         case TIFF_FLOAT:
  643.             TIFFSwabArrayOfLong((u_long *)cp, dir->tdir_count);
  644.             break;
  645.         case TIFF_RATIONAL:
  646.         case TIFF_SRATIONAL:
  647.             TIFFSwabArrayOfLong((u_long *)cp, 2*dir->tdir_count);
  648.             break;
  649.         }
  650.     }
  651.     return (cc);
  652. bad:
  653.     TIFFError(tif->tif_name, "Error fetching data for field \"%s\"",
  654.         TIFFFieldWithTag(dir->tdir_tag)->field_name);
  655.     return (0);
  656. }
  657.  
  658. /*
  659.  * Fetch an ASCII item from the file.
  660.  */
  661. static
  662. TIFFFetchString(tif, dir, cp)
  663.     TIFF *tif;
  664.     TIFFDirEntry *dir;
  665.     char *cp;
  666. {
  667.     if (dir->tdir_count <= 4) {
  668.         u_long l = dir->tdir_offset;
  669.         if (tif->tif_flags & TIFF_SWAB)
  670.             TIFFSwabLong(&l);
  671.         bcopy(&l, cp, dir->tdir_count);
  672.         return (1);
  673.     }
  674.     return (TIFFFetchData(tif, dir, cp));
  675. }
  676.  
  677. /*
  678.  * Convert numerator+denominator to float.
  679.  */
  680. static int
  681. cvtRational(tif, dir, num, denom, rv)
  682.     TIFF *tif;
  683.     TIFFDirEntry *dir;
  684.     u_long num, denom;
  685.     float *rv;
  686. {
  687.     if (denom == 0) {
  688.         TIFFError(tif->tif_name,
  689.             "%s: Rational with zero denominator (num = %lu)",
  690.             TIFFFieldWithTag(dir->tdir_tag)->field_name, num);
  691.         return (0);
  692.     } else {
  693.         if (dir->tdir_type == TIFF_RATIONAL)
  694.             *rv = ((float)num / (float)denom);
  695.         else
  696.             *rv = ((float)(long)num / (float)(long)denom);
  697.         return (1);
  698.     }
  699. }
  700.  
  701. /*
  702.  * Fetch a rational item from the file
  703.  * at offset off and return the value
  704.  * as a floating point number.
  705.  */
  706. static float
  707. TIFFFetchRational(tif, dir)
  708.     TIFF *tif;
  709.     TIFFDirEntry *dir;
  710. {
  711.     u_long l[2];
  712.     float v;
  713.  
  714.     return (!TIFFFetchData(tif, dir, (char *)l) ||
  715.         !cvtRational(tif, dir, l[0], l[1], &v) ? 1. : v);
  716. }
  717.  
  718. /*
  719.  * Fetch a single floating point value
  720.  * from the offset field and return it
  721.  * as a native float.
  722.  */
  723. static float
  724. TIFFFetchFloat(tif, dir)
  725.     TIFF *tif;
  726.     TIFFDirEntry *dir;
  727. {
  728.     float v = (float)
  729.         TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
  730.     TIFFCvtIEEEFloatToNative(tif, 1, &v);
  731.     return (v);
  732. }
  733.  
  734. /*
  735.  * Fetch an array of BYTE or SBYTE values.
  736.  */
  737. static
  738. TIFFFetchByteArray(tif, dir, v)
  739.     TIFF *tif;
  740.     TIFFDirEntry *dir;
  741.     u_short v[];
  742. {
  743.  
  744.     if (dir->tdir_count <= 4) {
  745.         /*
  746.          * Extract data from offset field.
  747.          */
  748.         if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  749.             switch (dir->tdir_count) {
  750.             case 4: v[3] = dir->tdir_offset & 0xff;
  751.             case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
  752.             case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
  753.             case 1: v[0] = dir->tdir_offset >> 24;
  754.             }
  755.         } else {
  756.             switch (dir->tdir_count) {
  757.             case 4: v[3] = dir->tdir_offset >> 24;
  758.             case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
  759.             case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
  760.             case 1: v[0] = dir->tdir_offset & 0xff;
  761.             }
  762.         }
  763.         return (1);
  764.     } else
  765.         return (TIFFFetchData(tif, dir, (char *)v));    /* XXX */
  766. }
  767.  
  768. /*
  769.  * Fetch an array of SHORT or SSHORT values.
  770.  */
  771. static
  772. TIFFFetchShortArray(tif, dir, v)
  773.     TIFF *tif;
  774.     TIFFDirEntry *dir;
  775.     u_short v[];
  776. {
  777.     if (dir->tdir_count <= 2) {
  778.         if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  779.             switch (dir->tdir_count) {
  780.             case 2: v[1] = dir->tdir_offset & 0xffff;
  781.             case 1: v[0] = dir->tdir_offset >> 16;
  782.             }
  783.         } else {
  784.             switch (dir->tdir_count) {
  785.             case 2: v[1] = dir->tdir_offset >> 16;
  786.             case 1: v[0] = dir->tdir_offset & 0xffff;
  787.             }
  788.         }
  789.         return (1);
  790.     } else
  791.         return (TIFFFetchData(tif, dir, (char *)v));
  792. }
  793.  
  794. /*
  795.  * Fetch an array of LONG or SLONG values.
  796.  */
  797. static
  798. TIFFFetchLongArray(tif, dir, v)
  799.     TIFF *tif;
  800.     TIFFDirEntry *dir;
  801.     u_long v[];
  802. {
  803.     if (dir->tdir_count == 1) {
  804.         v[0] = dir->tdir_offset;
  805.         return (1);
  806.     } else
  807.         return (TIFFFetchData(tif, dir, (char *)v));
  808. }
  809.  
  810. /*
  811.  * Fetch an array of RATIONAL or SRATIONAL values.
  812.  */
  813. static
  814. TIFFFetchRationalArray(tif, dir, v)
  815.     TIFF *tif;
  816.     TIFFDirEntry *dir;
  817.     float v[];
  818. {
  819.     int ok = 0;
  820.     u_long *l;
  821.  
  822.     l = (u_long *)CheckMalloc(tif,
  823.         dir->tdir_count*tiffDataWidth[dir->tdir_type],
  824.         "to fetch array of rationals");
  825.     if (l) {
  826.         if (TIFFFetchData(tif, dir, (char *)l)) {
  827.             u_long i;
  828.             for (i = 0; i < dir->tdir_count; i++) {
  829.                 ok = cvtRational(tif, dir,
  830.                     l[2*i+0], l[2*i+1], &v[i]);
  831.                 if (!ok)
  832.                     break;
  833.             }
  834.         }
  835.         free((char *)l);
  836.     }
  837.     return (ok);
  838. }
  839.  
  840. /*
  841.  * Fetch an array of FLOAT values.
  842.  */
  843. static
  844. TIFFFetchFloatArray(tif, dir, v)
  845.     TIFF *tif;
  846.     TIFFDirEntry *dir;
  847.     float v[];
  848. {
  849.     if (TIFFFetchData(tif, dir, (char *)v)) {
  850.         TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
  851.         return (1);
  852.     } else
  853.         return (0);
  854. }
  855.  
  856. /*
  857.  * Fetch a tag that is not handled by special case code.
  858.  *
  859.  * NB: DOUBLE and UNDEFINED types are not handled.
  860.  */
  861. static
  862. TIFFFetchNormalTag(tif, dp)
  863.     TIFF *tif;
  864.     TIFFDirEntry *dp;
  865. {
  866.     static char mesg[] = "to fetch tag value";
  867.     int ok = 0;
  868.  
  869.     if (dp->tdir_count > 1) {        /* array of values */
  870.         char *cp = NULL;
  871.  
  872.         switch (dp->tdir_type) {
  873.         case TIFF_BYTE:
  874.         case TIFF_SBYTE:
  875.             /* NB: always expand BYTE values to shorts */
  876.             cp = CheckMalloc(tif,
  877.                 dp->tdir_count * sizeof (u_short), mesg);
  878.             ok = cp && TIFFFetchByteArray(tif, dp, (u_short *)cp);
  879.             break;
  880.         case TIFF_SHORT:
  881.         case TIFF_SSHORT:
  882.             cp = CheckMalloc(tif,
  883.                 dp->tdir_count * sizeof (u_short), mesg);
  884.             ok = cp && TIFFFetchShortArray(tif, dp, (u_short *)cp);
  885.             break;
  886.         case TIFF_LONG:
  887.         case TIFF_SLONG:
  888.             cp = CheckMalloc(tif,
  889.                 dp->tdir_count * sizeof (u_long), mesg);
  890.             ok = cp && TIFFFetchLongArray(tif, dp, (u_long *)cp);
  891.             break;
  892.         case TIFF_RATIONAL:
  893.         case TIFF_SRATIONAL:
  894.             cp = CheckMalloc(tif,
  895.                 dp->tdir_count * sizeof (float), mesg);
  896.             ok = cp && TIFFFetchRationalArray(tif, dp, (float *)cp);
  897.             break;
  898.         case TIFF_FLOAT:
  899.             cp = CheckMalloc(tif,
  900.                 dp->tdir_count * sizeof (float), mesg);
  901.             ok = cp && TIFFFetchFloatArray(tif, dp, (float *)cp);
  902.             break;
  903.         case TIFF_ASCII:
  904.             /*
  905.              * Some vendors write strings w/o the trailing
  906.              * NULL byte, so always append one just in case.
  907.              */
  908.             cp = CheckMalloc(tif, dp->tdir_count+1, mesg);
  909.             if (ok = (cp && TIFFFetchString(tif, dp, cp)))
  910.                 cp[dp->tdir_count] = '\0';    /* XXX */
  911.             break;
  912.         }
  913.         if (ok)
  914.             ok = TIFFSetField(tif, dp->tdir_tag, cp);
  915.         if (cp != NULL)
  916.             free(cp);
  917.     } else if (CheckDirCount(tif, dp, 1)) {    /* singleton value */
  918.         char c[2];
  919.         switch (dp->tdir_type) {
  920.         case TIFF_BYTE:
  921.         case TIFF_SBYTE:
  922.         case TIFF_SHORT:
  923.         case TIFF_SSHORT:
  924.             ok = TIFFSetField(tif, dp->tdir_tag, (int)
  925.           TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset));
  926.             break;
  927.         case TIFF_LONG:
  928.         case TIFF_SLONG:
  929.             ok = TIFFSetField(tif, dp->tdir_tag, (u_long)
  930.           TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset));
  931.             break;
  932.         case TIFF_RATIONAL:
  933.         case TIFF_SRATIONAL:
  934.             ok = TIFFSetField(tif, dp->tdir_tag,
  935.                 TIFFFetchRational(tif, dp));
  936.             break;
  937.         case TIFF_FLOAT:
  938.             ok = TIFFSetField(tif, dp->tdir_tag,
  939.                 TIFFFetchFloat(tif, dp));
  940.             break;
  941.         case TIFF_ASCII:
  942.             if (ok = (TIFFFetchString(tif, dp, c))) {
  943.                 c[1] = '\0';        /* XXX paranoid */
  944.                 ok = TIFFSetField(tif, dp->tdir_tag, c);
  945.             }
  946.             break;
  947.         }
  948.     }
  949.     return (ok);
  950. }
  951.  
  952. /*
  953.  * Fetch samples/pixel short values for 
  954.  * the specified tag and verify that
  955.  * all values are the same.
  956.  */
  957. static
  958. TIFFFetchPerSampleShorts(tif, dir, pl)
  959.     TIFF *tif;
  960.     TIFFDirEntry *dir;
  961.     long *pl;
  962. {
  963.     u_short v[4];
  964.     int samples = tif->tif_dir.td_samplesperpixel;
  965.  
  966.     if (CheckDirCount(tif, dir, (u_long)samples) &&
  967.         TIFFFetchShortArray(tif, dir, v)) {
  968.         int i;
  969.         for (i = 1; i < samples; i++)
  970.             if (v[i] != v[0]) {
  971.                 TIFFError(tif->tif_name,
  972.         "Cannot handle different per-sample values for field \"%s\"",
  973.                    TIFFFieldWithTag(dir->tdir_tag)->field_name);
  974.                 return (0);
  975.             }
  976.         *pl = v[0];
  977.         return (1);
  978.     }
  979.     return (0);
  980. }
  981.  
  982. /*
  983.  * Fetch a set of offsets or lengths.
  984.  * While this routine says "strips",
  985.  * in fact it's also used for tiles.
  986.  */
  987. static
  988. TIFFFetchStripThing(tif, dir, nstrips, lpp)
  989.     TIFF *tif;
  990.     TIFFDirEntry *dir;
  991.     long nstrips;
  992.     u_long **lpp;
  993. {
  994.     register u_long *lp;
  995.     int status;
  996.  
  997.     if (!CheckDirCount(tif, dir, nstrips))
  998.         return (0);
  999.     /*
  1000.      * Allocate space for strip information.
  1001.      */
  1002.     if (*lpp == NULL &&
  1003.         (*lpp = (u_long *)CheckMalloc(tif,
  1004.           nstrips * sizeof (u_long), "for strip array")) == NULL)
  1005.         return (0);
  1006.     lp = *lpp;
  1007.     if (dir->tdir_type == (int)TIFF_SHORT) {
  1008.         /*
  1009.          * Handle short->long expansion.
  1010.          */
  1011.         u_short *dp = (u_short *)CheckMalloc(tif,
  1012.             dir->tdir_count* sizeof (u_short), "to fetch strip tag");
  1013.         if (dp == NULL)
  1014.             return (0);
  1015.         if (status = TIFFFetchShortArray(tif, dir, dp)) {
  1016.             register u_short *wp = dp;
  1017.             while (nstrips-- > 0)
  1018.                 *lp++ = *wp++;
  1019.         }
  1020.         free((char *)dp);
  1021.     } else
  1022.         status = TIFFFetchLongArray(tif, dir, lp);
  1023.     return (status);
  1024. }
  1025.  
  1026. #ifdef COLORIMETRY_SUPPORT
  1027. static
  1028. TIFFFetchRefBlackWhite(tif, dir)
  1029.     TIFF *tif;
  1030.     TIFFDirEntry *dir;
  1031. {
  1032.     static char mesg[] = "for \"ReferenceBlackWhite\" array";
  1033.     char *cp;
  1034.     int ok;
  1035.  
  1036.     if (!CheckDirCount(tif, dir, 2*tif->tif_dir.td_samplesperpixel))
  1037.         return (0);
  1038.     if (dir->tdir_type == TIFF_RATIONAL)
  1039.         return (TIFFFetchNormalTag(tif, dir));
  1040.     /*
  1041.      * Handle LONG's for backward compatibility.
  1042.      */
  1043.     cp = CheckMalloc(tif, dir->tdir_count * sizeof (u_long), mesg);
  1044.     if (ok = (cp && TIFFFetchLongArray(tif, dir, (u_long *)cp))) {
  1045.         float *fp = (float *)
  1046.             CheckMalloc(tif, dir->tdir_count * sizeof (float), mesg);
  1047.         if (ok = (fp != NULL)) {
  1048.             int i;
  1049.             for (i = 0; i < dir->tdir_count; i++)
  1050.                 fp[i] = (float)((u_long *)cp)[i];
  1051.             ok = TIFFSetField(tif, dir->tdir_tag, fp);
  1052.             free((char *)fp);
  1053.         }
  1054.     }
  1055.     if (cp)
  1056.         free(cp);
  1057.     return (ok);
  1058. }
  1059. #endif
  1060.  
  1061. #ifdef JPEG_SUPPORT
  1062. /*
  1063.  * Fetch the JPEG Quantization tables
  1064.  * for the specified directory entry.
  1065.  * Storage for the td_qtab array is
  1066.  * allocated as a side effect.
  1067.  */
  1068. static
  1069. TIFFFetchJPEGQTables(tif, dir)
  1070.     TIFF *tif;
  1071.     TIFFDirEntry *dir;
  1072. {
  1073.     TIFFDirectory *td = &tif->tif_dir;
  1074.     long off[4];
  1075.     int i, j;
  1076.     TIFFDirEntry tdir;
  1077.     char *qmat;
  1078.  
  1079.     if (dir->tdir_count > 1) {
  1080.         /* XXX verify count <= 4 */
  1081.         if (!TIFFFetchData(tif, dir, (char *)off))
  1082.             return (0);
  1083.     } else
  1084.         off[0] = dir->tdir_offset;
  1085.     /*
  1086.      * We don't share per-component q matrices because
  1087.      * (besides complicating this logic even more), it
  1088.      * would make it very painful if the user does a ``set''.
  1089.      */
  1090.     td->td_qtab = (u_char **)CheckMalloc(tif,
  1091.         dir->tdir_count*(sizeof (u_char *) + 64*sizeof (u_char)),
  1092.         "for JPEG Q table");
  1093.     if (td->td_qtab == NULL)
  1094.         return (0);
  1095.     tdir.tdir_type = TIFF_BYTE;
  1096.     tdir.tdir_count = 64;
  1097.     qmat = (((char *)td->td_qtab) + dir->tdir_count*sizeof (u_char *));
  1098.     for (i = 0; i < dir->tdir_count; i++) {
  1099.         td->td_qtab[i] = (u_char *)qmat;
  1100.         tdir.tdir_offset = off[i];
  1101.         if (!TIFFFetchData(tif, &tdir, qmat))
  1102.             return (0);
  1103.         qmat += 64*sizeof (u_char);
  1104.     }
  1105.     return (1);
  1106. }
  1107.  
  1108. /*
  1109.  * Fetch JPEG Huffman code tables for the
  1110.  * specified directory entry.  Storage for
  1111.  * the tables are allocated as a side effect.
  1112.  */
  1113. static
  1114. TIFFFetchJPEGCTables(tif, dir, ptab)
  1115.     TIFF *tif;
  1116.     TIFFDirEntry *dir;
  1117.     u_char ***ptab;
  1118. {
  1119.     long off[4];
  1120.     int i, j, ncodes;
  1121.     TIFFDirEntry tdir;
  1122.     char *tab;
  1123.  
  1124.     if (dir->tdir_count > 1) {
  1125.         /* XXX verify count <= 4 */
  1126.         if (!TIFFFetchData(tif, dir, (char *)off))
  1127.             return (0);
  1128.     } else
  1129.         off[0] = dir->tdir_offset;
  1130.     /*
  1131.      * We don't share per-component tables because
  1132.      * (besides complicating this logic even more), it
  1133.      * would make it very painful if the user does a
  1134.      * ``set''.  Note also that we don't try to optimize
  1135.      * storage of the tables -- we just allocate enough
  1136.      * space to hold the largest possible.  All this
  1137.      * stuff is so complicated 'cuz the tag is defined
  1138.      * to be compatible with the JPEG table format,
  1139.      * rather than something that fits well into the
  1140.      * structure of TIFF -- argh!
  1141.      */
  1142.     *ptab = (u_char **)CheckMalloc(tif, dir->tdir_count*
  1143.         (sizeof (u_char *) + (16+256)*sizeof (u_char)),
  1144.         "for JPEG Huffman table");
  1145.     if (*ptab == NULL)
  1146.         return (0);
  1147.     tdir.tdir_type = TIFF_BYTE;
  1148.     tab = (((char *)*ptab) + dir->tdir_count*sizeof (u_char *));
  1149.     for (i = 0; i < dir->tdir_count; i++) {
  1150.         (*ptab)[i] = (u_char *)tab;
  1151.         tdir.tdir_offset = off[i];
  1152.         tdir.tdir_count = 16;
  1153.         /*
  1154.          * We must fetch the array that holds the
  1155.          * count of codes for each bit length first
  1156.          * and the count up the number of codes that
  1157.          * are in the variable length table.  This
  1158.          * information is implicit in the JPEG format
  1159.          * 'cuz it's preceded by a length field.
  1160.          */
  1161.         if (!TIFFFetchData(tif, &tdir, tab))    /* count array */
  1162.             return (0);
  1163.         for (ncodes = 0, j = 0; j < 16; j++)
  1164.             ncodes += tab[j];
  1165.         /*
  1166.          * Adjust offsets and fetch codes separately.
  1167.          */
  1168.         tdir.tdir_offset += 16;
  1169.         tdir.tdir_count = ncodes;
  1170.         tab += 16;
  1171.         if (!TIFFFetchData(tif, &tdir, tab))
  1172.             return (0);
  1173.         tab += ncodes;
  1174.     }
  1175.     return (1);
  1176. }
  1177. #endif
  1178.  
  1179. /*
  1180.  * Accept matteing-only ExtraSamples tag.
  1181.  */
  1182. static
  1183. TIFFFetchExtraSamples(tif, dp)
  1184.     TIFF *tif;
  1185.     TIFFDirEntry *dp;
  1186. {
  1187.     int type;
  1188.     
  1189.     if (dp->tdir_count != 1) {
  1190.         TIFFError(tif->tif_name,
  1191.             "Can not handle more than 1 extra sample/pixel");
  1192.         return (0);
  1193.     }
  1194.     type = TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
  1195.     if (type != EXTRASAMPLE_ASSOCALPHA) {
  1196.         TIFFError(tif->tif_name,
  1197.             "Can only handle associated-alpha extra samples");
  1198.         return (0);
  1199.     }
  1200.     return (TIFFSetField(tif, TIFFTAG_MATTEING, 1));
  1201. }
  1202.